package com.linchuansi.fetureTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.*;

/**
 * *
 *
 * @author wb
 * @date 2025-11-22 21:53*@since 1.0.0
 */
public class CompletableFutureDemo {
    static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(5));
    static CountDownLatch countDownLatch = new CountDownLatch(3);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //supply();
        //allOf();
        //countDown();
        anyOf();
    }
    public static void anyOf()
    {
        Long start = System.currentTimeMillis();
        List<CompletableFuture> list = new ArrayList<>();


        list.add(CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行一");
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello World任务一";
        }, threadPoolExecutor));
        list.add(CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行二");
            try {
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            return "Hello World任务二";
            },threadPoolExecutor));
        list.add(CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行三");
            try {
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello World任务三";
        }, threadPoolExecutor));
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(list.toArray(new CompletableFuture[list.size()]));
        String join = anyOf.thenApplyAsync(s -> {
            System.out.println("接受到一个参数" + s);
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            s = (String) s;
            return "返回结果" + ((String) s).toUpperCase();
        }).join();
        System.out.println(join);

        //Object future = anyOf.join();
        //System.out.println(future);
        System.out.println("程序耗时"+(System.currentTimeMillis()-start));

    }

    public static void countDown() throws ExecutionException, InterruptedException {
        System.out.println("开始执行");
        Long start = System.currentTimeMillis();
        Future<String> feature1 =threadPoolExecutor.submit(() -> {
            System.out.println("开始执行一");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
            return "Hello World任务一";
        });
        Future<String> feature2 = threadPoolExecutor.submit(() -> {
            System.out.println("开始执行二");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
            return "Hello World任务二";
        });
        Future<String> feature3 = threadPoolExecutor.submit(() -> {
            System.out.println("开始执行三");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
            return "Hello World任务三";
        });
        try {
            countDownLatch.await();
            System.out.println("结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(feature1.get());
        System.out.println(feature2.get());
        System.out.println(feature3.get());
         System.out.println("程序运行时间：" + (System.currentTimeMillis() - start));
    }

    private static void supply() {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello World";
        }, threadPoolExecutor);
        completableFuture.thenAccept(s -> {
            System.out.println("接受到一个参数" + s);
            System.out.println("返回结果" + s.toLowerCase());
        });
        String s = completableFuture.join();
        System.out.println("最后输出" + s);
    }

    public static void allOf() {
        System.out.println("开始执行");
        Long start = System.currentTimeMillis();
        List<CompletableFuture<String>> list = new ArrayList<>();
        list.add(CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行一");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello World任务一";
        }, threadPoolExecutor));
        list.add(CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行二");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello World任务二";
        }, threadPoolExecutor));
        list.add(CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行三");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello World任务三";
        }, threadPoolExecutor));
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(list.toArray(new CompletableFuture[list.size()]));
        try {
            voidCompletableFuture.get(5000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.out.println("超时");
            e.printStackTrace();
        }

        System.out.println(list.get(0).join());
        System.out.println(list.get(1).join());
        System.out.println(list.get(2).join());
        System.out.println("结束");
        System.out.println("程序运行时间：" + (System.currentTimeMillis() - start));
    }
}
